<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\admin\controller\v1;

use app\admin\controller\BaseController;
use app\logic\RepairLogic;
use app\service\Wechat\WechatH5Service;
use app\model\UserOauth;
class Repair extends BaseController
{
    protected $name = '报修';

    protected $registerCurd = true;

    protected $registerCurdDir = true;

    protected $methodName = [
        'allotRepair' => '分配维修员',
        // 'getTotal'   => '获取缴费总汇'
    ];
    /**
     * 初始化
     */
    protected function initialize()
    {
        $this->logic = new RepairLogic();
    }

    /**
     * @Description: 分配维修员
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-05 10:12:44
     * @version: 1.0.0
     * @return [type]
     */    
    public function allotRepair(){

        $params = $this->request->post();
        
        $weachat = new WechatH5Service();

        $res = $this->logic->allotRepair($params);
        
        if($res){
            $openid = UserOauth::where('user_id','=',$params['repair_user_id'])->value('openid');
            $content = $this->logic->getRepairDetail($params);
            $data = [
                'touser'  => $openid,
                'time'    => date('Y-m-d h:i:s'),
                'user_id' => $params['repair_user_id'],
                'content' => '['.$content['location'].']'.$content['content'],
            ];
            $weachat->sendRepairMessage($data);
            return success($res,'分配成功');
        }else{
            error('分配失败');
        }
    }
}
